package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.DictDao;
import com.atguigu.dao.HouseDao;
import com.atguigu.entity.House;
import com.atguigu.service.HouseService;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Service(interfaceClass = HouseService.class)
@Transactional
public class HouseServiceImpl extends BaseServiceImpl<House> implements HouseService {

    @Autowired
    private HouseDao houseDao;
    @Autowired
    private DictDao dictDao;

    @Override
    protected BaseDao<House> getEntityDao() {
        return houseDao;
    }

    @Override
    public House getById(Serializable id) {
        House house = super.getById(id);
        //因为house里面很多都是存的ID,我们还需要根据ID查到具体NAME值放入house里再返回给前端.
        //houseTypeId to houseTypeName
        String houseTypeName = dictDao.getNameById(house.getHouseTypeId());
        house.setHouseTypeName(houseTypeName);
        //floorId to floorName
        String floorName = dictDao.getNameById(house.getFloorId());
        house.setFloorName(floorName);
        //buildStructureId to buildStructureName
        String buildStructureName = dictDao.getNameById(house.getBuildStructureId());
        house.setBuildStructureName(buildStructureName);
        //directionId to directionName
        String directionName = dictDao.getNameById(house.getDirectionId());
        house.setDirectionName(directionName);
        //decorationId to decorationName
        String decorationName = dictDao.getNameById(house.getDecorationId());
        house.setDecorationName(decorationName);
        //houseUseId to houseUseName
        String houseUseName = dictDao.getNameById(house.getHouseUseId());
        house.setHouseUseName(houseUseName);
        return house;
    }

    @Override
    public PageInfo<HouseVo> findListPage(Integer pageNum, Integer pageSize, HouseQueryVo houseQueryVo) {
        //开启分页插件
        PageHelper.startPage(pageNum,pageSize);
        //调用业务层查询数据库(开启分页插件后的第一个sql会自动加上limit语句)
        Page<HouseVo> pages= this.houseDao.findListPage(houseQueryVo);
        List<HouseVo> houseVoList = pages.getResult();
        for (HouseVo houseVo : houseVoList) {
            //根据里面需要查询的属性的id查询每个houseVo里面的dict名称(houseTypeName,floorName,directionName)
            houseVo.setHouseTypeName(dictDao.getNameById(houseVo.getHouseTypeId()));
            houseVo.setFloorName(dictDao.getNameById(houseVo.getFloorId()));
            houseVo.setDirectionName(dictDao.getNameById(houseVo.getDirectionId()));
        }
        //数据封装返回
        return new PageInfo<>(pages.getResult());
    }
}
